﻿package com.dLibs.photos
{	
	import caurina.transitions.Tweener;
	
	import com.dLibs.utils.scaleObject;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	public class SlideContent extends Sprite 
	{
		
		private var target_mc:Loader;
		private var mask_mc:MovieClip;
		
		private var largeX:Number;
		private var largura:Number;
		private var tol:Number = 133;
		private var vel_max:Number = 0.250000;
		private var vel_atual:Number = -0.050000;
		private var ymouse_min:Number;
		private var ymouse_max:Number;
		private var ymouse_atual:Number;
		private var y_min:Number = 0;
		private var y_atual:Number;
		private var y_max:Number;
		
		private var altura:Number;
		private var xmouse_min:Number;
		private var xmouse_max:Number;
		private var xmouse_atual:Number;
		private var x_min:Number = 0;
		private var x_atual:Number;
		private var x_max:Number;
		
		private var opened:Boolean = true;
		private var externalData:Boolean;
		private var internalData:DisplayObject;
		
		private var so:scaleObject;
		private var soResized:Boolean;
		
		public function SlideContent():void 
		{
			target_mc = new Loader();			
			mask_mc = getChildByName("maskObject") as MovieClip;
			
			addEventListener(Event.ENTER_FRAME, _enterFrame);
			addEventListener(Event.ADDED_TO_STAGE, _onAdded);
		}
		
		private function _onAdded(e:Event):void 
		{		
			ymouse_min = tol;
			ymouse_max = stage.stageHeight-tol;
			ymouse_atual = ymouse_min;
			y_atual = y_min;
			xmouse_min = tol;
			xmouse_max = stage.stageHeight-tol;
			xmouse_atual = xmouse_min;
			x_atual = x_min;
			largura = stage.stageWidth;
			altura = stage.stageHeight;
			
			mask_mc.width = largura;
			mask_mc.height = altura;
			
			super.addChildAt(target_mc, 0);
			mask = mask_mc;
		}
		
		private function _enterFrame(e:Event):void 
		{
			if (mask_mc.hitTestPoint(mouseX, mouseY)) 
			{
				
				if(!externalData){
					y_max = - (internalData.height - mask_mc.height);
					if (vel_atual<vel_max) {
						vel_atual = vel_atual+0.002500;
					}
					if (mouseX>0 && mouseX<largura) {
						ymouse_atual = mouseY;
					}
					if (ymouse_atual<ymouse_min) {
						ymouse_atual = ymouse_min;
					}
					if (ymouse_atual>ymouse_max) {
						ymouse_atual = ymouse_max;
					}
					y_atual = (ymouse_atual-ymouse_min)*y_max/(ymouse_max-ymouse_min);
					if (vel_atual>0) {
						internalData.y = internalData.y+(y_atual-internalData.y)*vel_atual;
					}
					//                                                                                                 
					x_max = -(internalData.width-mask_mc.width);
					if (vel_atual<vel_max) {
						vel_atual = vel_atual+0.002500;
					}
					if (mouseY>0 && mouseY<altura) {
						xmouse_atual = mouseX;
					}
					if (xmouse_atual<xmouse_min) {
						xmouse_atual = xmouse_min;
					}
					if (xmouse_atual>xmouse_max) {
						xmouse_atual = xmouse_max;
					}
					x_atual = (xmouse_atual-xmouse_min)*x_max/(xmouse_max-xmouse_min);
					if (vel_atual>0) {
						internalData.x = internalData.x+(x_atual-internalData.x)*vel_atual;
					}
					return;
				}
				
				y_max = - (target_mc.height - mask_mc.height);
				
				if (vel_atual<vel_max) 
				{
					vel_atual = vel_atual+0.002500;
				}
				if (mouseX>0 && mouseX<largura) {
					ymouse_atual = mouseY;
				}
				if (ymouse_atual<ymouse_min) {
					ymouse_atual = ymouse_min;
				}
				if (ymouse_atual>ymouse_max) {
					ymouse_atual = ymouse_max;
				}
				y_atual = (ymouse_atual-ymouse_min)*y_max/(ymouse_max-ymouse_min);
				if (vel_atual>0) {
					target_mc.y = target_mc.y+(y_atual-target_mc.y)*vel_atual;
				}
				//                                                                                                 
				x_max = -(target_mc.width-mask_mc.width);
				if (vel_atual<vel_max) {
					vel_atual = vel_atual+0.002500;
				}
				if (mouseY>0 && mouseY<altura) {
					xmouse_atual = mouseX;
				}
				if (xmouse_atual<xmouse_min) {
					xmouse_atual = xmouse_min;
				}
				if (xmouse_atual>xmouse_max) {
					xmouse_atual = xmouse_max;
				}
				x_atual = (xmouse_atual-xmouse_min)*x_max/(xmouse_max-xmouse_min);
				if (vel_atual>0) {
					target_mc.x = target_mc.x+(x_atual-target_mc.x)*vel_atual;
				}
			}
		}
	
		public function load(url:String):void 
		{
			if(url != ""){
				externalData = true;
				target_mc.load(new URLRequest(url));
				if(internalData != null){
					super.removeChild(internalData);
				}
			}
		}
		
		public function setSize(width:Number, height:Number):void 
		{
			largura = width;
			altura = height;
			
			if ( soResized )
			{
				// Tweener.addTween(target_mc, {width:so.oldWidth, height:so.oldHeight, time:1, transition:"easeInOutExpo"});
			}
			
			soResized = false;
			Tweener.addTween(mask_mc, {width:largura, height:altura, time:1, transition:"easeInOutExpo"});
		}
		
		public function setSizeFull():void
		{
			so = new scaleObject(target_mc, { width: stage.stageWidth, height: stage.stageHeight });
			soResized = true;
			Tweener.addTween(mask_mc, {width:stage.stageWidth, height:stage.stageHeight, time:1, transition:"easeInOutExpo"});
		}
		
		override public function addChild(child:DisplayObject):DisplayObject 
		{
			externalData = false;
			internalData = child;
			super.addChild(child);
			super.removeChild(target_mc);
			return child;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject 
		{
			externalData = true;
			super.removeChild(child);
			super.addChild(target_mc);
			return child;
		}
	}
}